Apache Wink : 5.9 Assets
This page last changed on Oct 13, 2009 by michael.
AssetsAn Asset is a special entity that is returned by a resource method or is injected into a resource method as an entity parameter. The asset is used for retrieving the actual request entity or response entity. When an asset instance is returned from a resource method or is set as the entity on a Response instance, it is used by the Apache Wink runtime to retrieve the actual response entity by invoking the appropriate entity-producing method of the asset.
When an asset is the entity parameter of a resource method, it is used by the Apache Wink runtime to set the actual request entity by invoking the appropriate entity-consuming method of the asset. Assets OverviewA typical application exposes each resource in a number of representations. Some form of data model usually backs the resource, and the application business logic relies on the manipulation of that data model. The application will most likely expose resource methods allowing the consumption of the data model in more than one representation (for example Atom and XML) and the production of the data model in other representation (for example Atom, XML and JSON). According to the JAX-RS specification, the optimal method for implementing a resource is one that consumes and produces an application data model and makes use of a different provider for every media type. For example, if a resource implements methods that consume and produce a "Defect" bean, then a provider must be implemented for each representation of the "Defect" (Atom, XML and JSON). However, there are times that the transformation of the application data model into a representation requires information that may only be available to the resource but is unavailable to a provider (for example, a connection to the Database). There are several solutions for dealing with the problem of a provider not having sufficient information to perform application data transformations. The following is a description of two possible solutions:
This solution is only plausible if the resource scope is "per request" and does not work if the resource is a singleton.
This solution is only plausible when the application is deployed in a JEE container and is not the optimal solution. As a result, the selection of the actual provider from the set of potential providers is non-deterministic, because the selection between them is undefined.
LifecycleResource methods can use an asset as a response entity and as a request entity. The Apache Wink runtime applies different lifecycles for each case. Response Entity AssetThe lifecycle of an asset as a response entity is as follows:
Request Entity AssetThe lifecycle of an asset as a request entity is as follows:
Asset Entity MethodsAsset Entity methods are the public methods of an asset annotated with either @Consumes or @Produces annotation. Annotating a method with both @Consumes and @Produces annotations is not supported and may result in unexpected behavior. Entity Producing MethodsAn Entity Producing Method is a public asset method annotated with the @Produces annotation, designating it to produce the actual response entity. Such methods produce an entity only for the media types declared in the @Produces annotation. Note that under this definition, wildcard ("/") is allowed. Entity Consuming MethodsAn Entity Consuming Method is a public asset method annotated with the @Consumes annotation, designating it to consume the actual request entity for populating the asset. Such methods consume an entity only for the media types declared in the @Consumes annotation. Note that under this definition, wildcard ("/") is allowed. ParametersAsset Entity methods support the same parameter types as JAX-RS specifies for a resource method. Return TypeEntity methods may return any type that is permissible to return from a resource method. ExceptionsExceptions thrown from an entity method are treated as exceptions thrown from a resource method. Annotation InheritanceThe @Produces and @Consumes annotations are not inherited when an asset sub-class overrides an asset entity method. Asset sub-classes must re-declare the @Produces and @Consumes annotations for the overriding method to be an entity method. Entity Method MatchingAsset classes are handled by the AssetProvider which is a JAX-RS provider that is capable of consuming and producing all media types.
Request Entity MatchingThe following points describe the process of selecting the asset entity-consuming method to handle the request entity. This process occurs during the invocation of the AssetProvider#isReadable() method.
Response Entity MatchingThe following points describe the process of selecting an entity-producing method to produce the actual response entity. The following process occurs during the invocation of the AssetProvider#isWriteable() method.
Asset ExampleThe following example illustrates the use of an asset. The "Defect" bean is a JAXB annotated class. DefectAsset ClassThe DefectAsset class is the asset backed by an instance of a "Defect" bean. @Asset public class DefectAsset { public Defect defect; public DefectAsset(Defect defect) { this.defect = defect; } @Produces("application/xml") public Defect getDefect() { return this.defect; } @Produces("text/html") public String getDefectAsHtml() { String html = ...; return html; } @Produces("application/atom+xml") public AtomEntry getDefectAsAtom() { AtomEntry entry = ...; return entry; } @Consumes("application/xml") public void setDefect(Defect defect) { this.defect = defect; } } DefectResource ClassThe DefectResource class is a resource that is anchored to the URI path "defects/{id}" within the Apache Wink runtime. @Path("defects/{id}") public class DefectResource { @GET public DefectAsset getDefect(@PathParam("id") String id) { return new DefectAsset(defects.get(id)); } @PUT public DefectAsset updateDefect(DefectAsset defectAsset, @PathParam("id") String id) { defects.put(id, defectAsset.getDefect()); return defectAsset; } } Scenario Explanation 1
Scenario Explanation 2
Scenario Explanation 3
|
Document generated by Confluence on Nov 11, 2009 06:57 |